<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增用户</title>
    <link href="${basePath }/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <link href="${basePath}/static/plugins/email-auto-complete/emailAutoComplete.css" rel="stylesheet">
    <style>
        .upload-img{
            width: 100px;
            height: 100px;
            border-radius: 50%;
            margin-top: 8px;
        }
        input.upload-file {
            position: absolute;
            top: 0px;
            left: 0;
            height: 39px;
            width: 94px;
            opacity: 0;
            cursor: pointer;
        }
    </style>
</head>
<body>
<form id="user-form" class="layui-form" action="javascript:;" style="margin: 24px 48px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <div class="parentCls">
                <input type="text" name="email" placeholder="请输入邮箱" class="layui-input inputElem">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入用户名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="nickname" placeholder="请输入昵称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">锁定</label>
        <div class="layui-input-block">
            <input type="checkbox" name="lock" lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-primary">选择头像</button>
                <input class="upload-file" type="file" name="file" id="upload" onchange="preImg(this.id,'upload-img');" >
                <div class="upload-list">
                    <img class="upload-img" id="upload-img" src="${basePath}/static/build/images/avatar.jpg">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <c:forEach items="${roles}" var="r">
                <input type="checkbox" lay-filter="roleId" name="roleId" title="${r.roleName}" value="${r.roleId}">
            </c:forEach>

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="user-form">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${basePath }/static/plugins/layui/layui.js"></script>
<script src="${basePath }/static/plugins/jq/jquery-2.2.4.min.js"></script>
<script src="${basePath }/static/build/js/common.js"></script>
<script>
    layui.use(['form', 'layer', 'laydate', 'upload'], function() {
        var form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            layer = layui.layer,
            roleIdArr = [];

        form.on('checkbox(roleId)', function(data){
            console.log(data.value);
            if (data.elem.checked){
                roleIdArr.push(data.value);
            } else {
                roleIdArr.splice($.inArray('c',roleIdArr),1);
            }

        });

        form.render();
        form.on('submit(user-form)', function(data){
            var reqData = data.field;
            console.log(reqData);
            var formData = new FormData($('form')[0]);
            formData.append('roleIds', roleIdArr.join(','));
            $.ajax({
                url: '${basePath}/admin/user/insert',
                type: 'post',
                dataType: 'json',
                data: formData,
                processData: false,
                contentType: false,
                cache: false,
                success: function(resp){
                    if(resp.code==10000){
                        layer.msg('新增成功',{
                            time: 1000
                        }, function(){
                            window.location.reload();
                        })
                    }
                }
            })
            return false;
        });
    });


    /**
     * 从 file 域获取 本地图片 url
     */
    function getFileUrl(sourceId) {
        var url;
        if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
            url = document.getElementById(sourceId).value;
        } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
            url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
        } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
            url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
        }
        return url;
    }

    /**
     * 将本地图片 显示到浏览器上
     */
    function preImg(sourceId, targetId) {
        var url = getFileUrl(sourceId);
        var imgPre = document.getElementById(targetId);
        imgPre.src = url;
    }

</script>
</body>
</html>
